package edu.clemson.wcbeck.cft;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class CFTMediaGallery extends Activity{

	private static final String urlServer0 = "http://192.168.1.100/wcbeck/uploadImage.php";
	private static final String urlServer1 = "http://192.168.1.100/wcbeck/uploadVideo.php";
	private static final String urlServer2 = "http://192.168.1.100/wcbeck/uploadAudio.php";
	
	private MyDatabaseHelper dataSource;
	private ImageAdapter mMediaAdapter;
	Toast myToast;
	CharSequence toastText;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.mediagallery);
	    
	    GridView gridview = (GridView) findViewById(R.id.gridview);
	    mMediaAdapter = new ImageAdapter(this);
	    gridview.setAdapter(mMediaAdapter);

	    gridview.setOnItemClickListener(new OnItemClickListener() {
	    	
	        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	        	
	            Toast.makeText(CFTMediaGallery.this, "" + position, Toast.LENGTH_SHORT).show();
	        }
	    });
	    
	    dataSource = new MyDatabaseHelper(this);
        try {
			dataSource.createDataBase();
		} catch (IOException e) {
			toastText = "Error creating the database!";
			myToast = Toast.makeText(this.getApplicationContext(), toastText, Toast.LENGTH_SHORT);
			myToast.show();
			e.printStackTrace();
		}
	    
	    registerForContextMenu(gridview);
	}
	
	
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    	if (v.getId() == R.id.gridview) {
    	    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    	    
    	    MediaFile mediaFile = mMediaAdapter.getMediaList().get(info.position);
    	    
    	    String[] menuItems = getResources().getStringArray(R.array.strArrContextMenuGallery);
    	    menu.setHeaderTitle(mediaFile.getName() + " options:");
    	    
    	    for (int i = 0; i<menuItems.length; i++) {
    	      menu.add(Menu.NONE, i, i, menuItems[i]);
    	    }
    	  }
    }
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		dataSource.openDataBase();

		MediaFile mediaFile = mMediaAdapter.getMediaList().get(info.position);

		switch (menuItemIndex) {
		case 0: // Delete
			break;
		case 1: // Details
			if (mediaFile.getIsUploaded() == 1)
				toastText = "Image - Uploaded Already";
			else
				toastText = "Image - Not Uploaded Yet";
			myToast = Toast.makeText(this.getApplicationContext(), toastText,Toast.LENGTH_SHORT);
			myToast.show();
			break;
		case 2: // Upload
			String urlServer = urlServer0;
			String fileName = mediaFile.getDate_Time() + ".jpg";
			switch(mediaFile.getMediaType()){
			case 0: /*Images*/
				fileName = mediaFile.getDate_Time() + ".jpg";
				urlServer = urlServer0;
				break;
			case 1: /*Video*/
				fileName = mediaFile.getDate_Time() + ".mp4";
				urlServer = urlServer1;
				break;
			case 2: /*Audio*/
				fileName = mediaFile.getDate_Time() + ".3gp";
				urlServer = urlServer2;
				break;
			}
			try {
			    InputStream in = new FileInputStream(new File(mediaFile.getLocation())); 
			    Log.d("Upload Demo", "Gallery Activity");
			    uploadHttpFile htfu = new uploadHttpFile(urlServer, "noparamshere", fileName);
				htfu.doStart(in);
				
				toastText = "Upload was successful!";
				myToast = Toast.makeText(this.getApplicationContext(), toastText,Toast.LENGTH_SHORT);
				myToast.show();
				break;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				toastText = "Upload failed!";
				myToast = Toast.makeText(this.getApplicationContext(), toastText,Toast.LENGTH_SHORT);
				myToast.show();
				e.printStackTrace();
			}
		}

		dataSource.close();
		return true;
	}
	
}
